package com.aier.admin_new.action;

import java.io.File;
import java.io.IOException;
import java.util.zip.ZipException;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.admin_new.common.CompresszZipFile;
import com.aier.admin_new.common.FileToolkit;
import com.aier.common.common.ImageUtil;
import com.aier.common.common.SystemConfigStatic;
import com.aier.school.service.ISchoolService;
import com.aier.user.service.IUserService;

/**
 * APP端所有上传图片的action
 * @author Wkk300
 */
@Scope("prototype")
@Component("imageAppAction")
public class ImageAppAction extends AdminNewBaseAction{

	 	@Resource(name = "userService")
	    private IUserService userService;
	    @Resource(name = "schoolService")
	    private ISchoolService schoolService;

	    private int s; // 成功:1 ,失败:0
	    private String error_info;
	    private String headimg; // 头像图片名
	    private Long uid;
	    private File file;//上传的头像图片
	    private Long dfid;            // 餐饮id
	    
	    // 图片原名
	    private String photoName;
	    private int x;
	    private int y;
	    private int w;
	    private int h;
	    
	    @Override
	    public String execute() {
	        return SUCCESS;
	    }
	    
	 //app修改用户头像
    public String updateUserHImgApp(){
    	headimg = ImageUtil.uploadImage(file, upload_picturl_url, photoName, 1);
    	//ImageUtil.cutImg(SystemConfigStatic.upload_picturl_url, headimg, x,y, w, h);
    	if (userService.updateUserHeadImg(uid, headimg) == 0) {
            s = 0;
            error_info = "修改用户头像失败";
        } else {
            s = 1;
        }
    	 return SUCCESS;
    }

	 //app更新教师风采或者是用户注册时候用
    public String updateTeacherCharismaApp(){
    	headimg = ImageUtil.uploadImage(file, upload_picturl_url, photoName, 1);
    	//ImageUtil.cutImg(SystemConfigStatic.upload_picturl_url, headimg, x,y, w, h);
    	return SUCCESS;
    }
    
    /**
     * 教师客户端上传餐饮图片app 网页可能也能用
     * @return
     */
    public String uploadDailyfoodImgApp() {

        String picName = "";
        String folderPath =temp_upload_url;
        try {      
        	String zippath=folderPath+Math.random()+"zip";// 根据路径名创建一个放zip的文件夹
    		File filea = new File(zippath);
    		if(!filea.exists()){
    			filea.mkdir();
    		}
    		
        	FileUtils.copyFile(file, new File(filea, photoName));
        	File fileazip = new File(filea+"/"+photoName);  //路径zip文件名
        	String zipImgpath = folderPath+Math.random()+"jpg";//存放图片的文件夹
    		File fileb = new File(zipImgpath);	//解压到某个文件夹下
    		 if(fileb.exists()){
    			 fileb.mkdir();
    		 }
    		 
    		 CompresszZipFile.unzip(fileazip, zipImgpath);
    		 
			//读取这个文件夹下的所有图片
			 File f = new File(zipImgpath);
			 File [] files = f.listFiles();
	         for (int i = 0; i < files.length; i++){
	             File file1 = files[i];
	             String fileName = ImageUtil.uploadImage(file1,SystemConfigStatic.caipu_upload_url, file1.getName(),(int) 1);
	             if (!"".equals(picName)) {
                   picName += ",";
              }
	             picName += fileName;
	        } 
	         s = schoolService.insertDailyFoodImg(dfid, picName);
	         headimg=picName;
	       //删除这个zip文件夹
	        FileToolkit.deleteFile(zipImgpath);
			FileToolkit.deleteFile(zippath);
		} catch (ZipException e) {
			s=0;
			e.printStackTrace();
		} catch (IOException e) {
			s=0;
			e.printStackTrace();
		}
        return SUCCESS;
    }
       
    
	public Long getUid() {
		return uid;
	}

	public void setUid(Long uid) {
		this.uid = uid;
	}

	public int getS() {
		return s;
	}

	public String getError_info() {
		return error_info;
	}

	public void setHeadimg(String headimg) {
		this.headimg = headimg;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getHeadimg() {
		return headimg;
	}

	public void setPhotoName(String photoName) {
		this.photoName = photoName;
	}

	public void setDfid(Long dfid) {
		this.dfid = dfid;
	}
}
